<template>
  <div id="userInfoModify">
    <h2 class="title">云图库-用户信息修改</h2>
    <div class="desc">企业级智能协同云图库</div>
    <a-form :model="formState" name="loginForm" autocomplete="off" @finish="submit">

      <!-- 在 a-form 中添加隐藏的 id 表单项 -->
      <a-form-item name="id" style="display: none;">
        <a-input v-model:value="formState.id" />
      </a-form-item>

      <a-form-item name="userName" :rules="[{ required: true, message: '用户名不能为空' }]">
        <a-input v-model:value="formState.userName" placeholder="请输入用户名" />
      </a-form-item>

      <a-form-item name="userProfile">
        <a-input v-model:value="formState.userProfile" placeholder="请输入个人简介" />
      </a-form-item>


      <a-form-item name="userAvatar" >
        <a-input v-model:value="formState.userAvatar" placeholder="修改头像，暂不支持" disabled/>
      </a-form-item>

      <!--      提交按钮必须添加html-type-->
      <a-form-item :wrapper-col="{ offset: 8, span: 16 }">
        <a-button type="primary" html-type="submit">修改用户信息</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import { useLoginUserStore } from '@/stores/userLoginUserStore.ts'
import { updUserUsingPost } from '@/api/userController.ts'
import { message } from 'ant-design-vue'
import router from '@/router'

const loginUserStore = useLoginUserStore()

// 页面回显
const formState = reactive<API.UserUpdateRequest>({
  userName: loginUserStore.loginUser.userName ?? '',
  userProfile: loginUserStore.loginUser.userProfile ?? '',
  id:loginUserStore.loginUser.id ?? ''
})

//页面上点击提交按钮触发，调用后端修改用户信息接口
const submit = async (values: any) => {
  //调用后端登录接口
  const resp = await updUserUsingPost(values)
  if (resp.data.code === 0) {
    //修改成功，重新获取用户信息
    await loginUserStore.fetchLoginUser()
    message.success('修改成功')
    //跳转回首页
    router.push({
      path: '/',
      replace: true,
    })
  } else {
    message.error(resp.data.message)
  }
}
</script>

<style scoped>
#userInfoModify {
  max-width: 360px;
  margin: 0 auto;
}

.title {
  text-align: center;
  margin-bottom: 16px;
}

.desc {
  text-align: center;
  margin-bottom: 16px;
  color: #bbb;
}

.tips {
  text-align: center;
  margin-bottom: 16px;
  font-size: 13px;
  color: #bbb;
}
</style>
